/**
  ******************************************************************************
  * @file    BSP_uart.c
  * @author  Silan - MC Team
  * @version 1.0.0
  * @date    2017/07/19
  * @brief   UART Related Config Source File
  * @details None
	* @note    None
  ******************************************************************************
  * @attention
  * THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
  * AS A RESULT, SILAN MICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
  * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
  * CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
  * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
	*
  ******************************************************************************
	* Change History
	* -# 2017/07/19 | v1.0.0 | Wangjianlong | Creat file.
	*
	******************************************************************************
  */

/*******************************************************************************
 * Include file
 ******************************************************************************/
#include "BSP_uart.h"


/*******************************************************************************
 * Global data for the project
 ******************************************************************************/


/*******************************************************************************
 * Local data for the current file
 ******************************************************************************/


/*******************************************************************************
 * Local prototypes function
 ******************************************************************************/


/*******************************************************************************
 * Local function for the current file
 ******************************************************************************/


/*******************************************************************************
 * Global function for the project
 ******************************************************************************/

/**
  * @brief     UART0 Initialization.
  * @param[in] BaudRate : Baud Rate
  */
void Uart0Init(uint32_t BaudRate)
{
	UART0->UARTRSR  = 0;                // Clear all error status
	UART0->UARTIBRD = (SystemApb01Clock/(16*BaudRate));
	UART0->UARTFBRD = (((SystemApb01Clock%(16*BaudRate))<<7)+(16*BaudRate))/(BaudRate<<5);   //(0.XX*64+0.5)
	UART0->UARTLCR_H_b.SPS = 0;         // No odd or even check
	UART0->UARTLCR_H_b.WLEN = 3;        // 8BIT
	UART0->UARTLCR_H_b.FEN = 0;         // FIFO Disable
	UART0->UARTLCR_H_b.PEN = 0;         // Parity Disable
	UART0->UARTLCR_H_b.STP2 = 0;        // 0:1 stop bit, 1:2 stop bits

	UART0->UARTIMSC_b.Receive_IM = 1;   // RX Int Enable
	UART0->UARTIMSC_b.OE_IM = 1;        // Overflow Error Int Enable

	UART0->UARTICR = 0x7ff;             // Clear all interrupt flags

	UART0->UARTCR_b.RXE = 1;            // RX Enable
	UART0->UARTCR_b.TXE = 1;            // TX Enable
	UART0->UARTCR_b.UARTEN = 1;         // UARTx Enable
}

/**
  * @brief  UART1 Initialization.
  */
void Uart1Init(uint32_t BaudRate)
{
	UART1->UARTRSR  = 0;                // Clear all error status
	UART1->UARTIBRD = (SystemApb01Clock/(16*BaudRate));
	UART1->UARTFBRD = (((SystemApb01Clock%(16*BaudRate))<<7)+(16*BaudRate))/(BaudRate<<5);   //(0.XX*64+0.5)
	UART1->UARTLCR_H_b.SPS = 0;         // No odd or even check
	UART1->UARTLCR_H_b.WLEN = 3;        // 8BIT
	UART1->UARTLCR_H_b.FEN = 0;         // FIFO Disable
	UART1->UARTLCR_H_b.PEN = 0;         // Parity Disable
	UART1->UARTLCR_H_b.STP2 = 0;        // 0:1 stop bit, 1:2 stop bits

	UART1->UARTIMSC_b.Receive_IM = 1;   // RX Int Enable
	UART1->UARTIMSC_b.Transmit_IM = 1;  // TX Int Enable
	UART1->UARTIMSC_b.OE_IM = 1;        // Overflow Error Int Enable

	UART1->UARTICR = 0x7ff;             // Clear all interrupt flags

	UART1->UARTCR_b.RXE = 1;            // RX Enable
	UART1->UARTCR_b.TXE = 1;            // TX Enable
	UART1->UARTCR_b.UARTEN = 1;         // UARTx Enable
}

/**
  * @brief  UART2 Initialization.
  */
void Uart2Init(uint32_t BaudRate)
{
	UART2->UARTRSR  = 0;                // Clear all error status
	UART2->UARTIBRD = (SystemApb01Clock/(16*BaudRate));
	UART2->UARTFBRD = (((SystemApb01Clock%(16*BaudRate))<<7)+(16*BaudRate))/(BaudRate<<5);   //(0.XX*64+0.5)
	UART2->UARTLCR_H_b.SPS = 0;         // No odd or even check
	UART2->UARTLCR_H_b.WLEN = 3;        // 8BIT
	UART2->UARTLCR_H_b.FEN = 1;         // FIFO Enable
	UART2->UARTIFLS_b.RXIFLSEL = 4;     // 7/8 FIFO
	UART2->UARTIFLS_b.TXIFLSEL = 6;     // 7/8 FIFO
	UART2->UARTLCR_H_b.PEN = 0;         // Parity Disable
	UART2->UARTLCR_H_b.STP2 = 0;        // 0:1 stop bit, 1:2 stop bits

	UART2->UARTIMSC_b.Receive_IM = 1;   // RX Int Enable
	UART2->UARTIMSC_b.RT_IM = 1;        // RX timerout Int Enable
	UART2->UARTIMSC_b.Transmit_IM = 1;  // TX Int Enable
	UART2->UARTIMSC_b.OE_IM = 1;        // Overflow Error Int Enable

	UART2->UARTICR = 0x7ff;             // Clear all interrupt flags

	UART2->UARTCR_b.RXE = 1;            // RX Enable
	UART2->UARTCR_b.TXE = 1;            // TX Enable
	UART2->UARTCR_b.UARTEN = 1;         // UARTx Enable
}

/*---------------------------------- End of the file ---------------------------------*/
